package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"html/template"
	"net/http"
	"os"
	"path/filepath"
)

type User1 struct {
	Name   string
	Gender string
	Age    int
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	//解析模板

	//渲染模板
	t, err := template.ParseFiles("./hello.tmpl")
	name := "zlx"
	if err != nil {
		fmt.Println("err%v", err)
	}
	t.Execute(w, name)

}
func index(w http.ResponseWriter, r *http.Request) {
	// 定义模板
	// 解析模板
	// 父模板和子模板的顺序不能乱，父在前，子在后
	t, err := template.ParseFiles("./layout/main.tmpl", "./layout/content.tmpl")
	if err != nil {
		fmt.Printf("parse files failed, err : %v\n", err)
		return
	}
	// 渲染模板
	// 渲染模板时使用ExecuteTemplate函数，需要制定要被渲染的模板名称
	t.ExecuteTemplate(w, "content.tmpl", "index")
}
func Goindex() {
	http.HandleFunc("/index", index)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Printf("http server failed, err : %v\n", err)
		return
	}
}
func ResfulApi(w http.ResponseWriter, r *http.Request) {
	t, err := template.ParseFiles("./hello.tmpl")
	if err != nil {
		fmt.Println("parse template failed,err %v", err)
	}
	u1 := User1{
		Name:   "zlx",
		Gender: "男",
		Age:    26,
	}
	u2 := User1{
		Name:   "lzx",
		Gender: "女",
		Age:    26,
	}
	m1 := map[string]interface{}{
		"u1": u1,
		"m1": u2,
	}
	t.Execute(w, m1)

}
func getCurrentPath() string {
	if ex, err := os.Executable(); err == nil {
		return filepath.Dir(ex)
	}
	return "./"
}
func goJson() {
	r := gin.Default()

	// gin.H 是map[string]interface{}的缩写
	r.GET("/someJSON", func(c *gin.Context) {
		// 方式一：自己拼接JSON
		c.JSON(http.StatusOK, gin.H{"message": "Hello world!"})
	})
	r.GET("/moreJSON", func(c *gin.Context) {
		// 方法二：使用结构体
		var msg struct {
			Name    string `json:"user"`
			Message string
			Age     int
		}
		msg.Name = "zlx"
		msg.Message = "Hellos world!"
		msg.Age = 26
		c.JSON(http.StatusOK, msg)
	})
	r.Run(":8080")
}
func goXml() {
	r := gin.Default()
	// gin.H 是map[string]interface{}的缩写
	r.GET("/someXML", func(c *gin.Context) {
		// 方式一：自己拼接JSON
		c.XML(http.StatusOK, gin.H{"message": "Hello world!"})
	})
	r.GET("/moreXML", func(c *gin.Context) {
		// 方法二：使用结构体
		type MessageRecord struct {
			Name    string
			Message string
			Age     int
		}
		var msg MessageRecord
		msg.Name = "zlx"
		msg.Message = "Hello world!"
		msg.Age = 26
		c.XML(http.StatusOK, msg)
	})
	r.Run(":8080")

}
func goApi() {
	http.HandleFunc("/hello", ResfulApi)
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		fmt.Println("server is starting go%v", err)
		return
	}
}
func GetParagram() {
	r := gin.Default()
	r.GET("/user/search", func(c *gin.Context) {
		username := c.DefaultQuery("username", "zlx")
		//username := c.Query("username")
		address := c.Query("address")
		//输出json结果给调用方
		c.JSON(http.StatusOK, gin.H{
			"message":  "ok",
			"username": username,
			"address":  address,
		})
	})
	r.Run()
}
func PostMessage() {
	r := gin.Default()
	r.POST("/user/search", func(c *gin.Context) {
		// DefaultPostForm取不到值时会返回指定的默认值
		//username := c.DefaultPostForm("username", "小王子")
		username := c.PostForm("username")
		address := c.PostForm("address")
		//输出json结果给调用方
		c.JSON(http.StatusOK, gin.H{
			"message":  "ok",
			"username": username,
			"address":  address,
		})
	})
	r.Run(":8080")
}
func main() {
	//goXml()
	//goJson()
	//GetParagram()

}
